package com.example.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private LinearLayout mainContainer;
    private Handler handler = new Handler();
    private int currentIndex = 0;
    private Runnable colorRunnable = this::updateColor;
    private int[] colors;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 初始化颜色数组
        colors = new int[]{
                getColor(android.R.color.holo_blue_dark),
                getColor(android.R.color.holo_green_dark),
                getColor(android.R.color.holo_orange_dark),
                getColor(android.R.color.holo_purple),
                getColor(android.R.color.holo_red_dark)
        };

        // 创建主容器
        mainContainer = new LinearLayout(this);
        mainContainer.setOrientation(LinearLayout.VERTICAL);
        mainContainer.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));
        setContentView(mainContainer);

        // 显示主菜单
        showMainMenu();
    }

    private void showMainMenu() {
        mainContainer.removeAllViews();

        // 添加标题
        Button titleButton = new Button(this);
        titleButton.setText("实验二 - 布局管理器");
        titleButton.setEnabled(false);
        titleButton.setTextSize(20);
        mainContainer.addView(titleButton);

        // 添加UC1按钮
        Button uc1Button = new Button(this);
        uc1Button.setText("UC1 - 线性布局");
        uc1Button.setOnClickListener(v -> showLinearLayoutDemo());
        mainContainer.addView(uc1Button);

        // 添加UC2按钮
        Button uc2Button = new Button(this);
        uc2Button.setText("UC2 - 相对布局");
        uc2Button.setOnClickListener(v -> showRelativeLayoutDemo());
        mainContainer.addView(uc2Button);

        // 添加UC3按钮
        Button uc3Button = new Button(this);
        uc3Button.setText("UC3 - 帧布局");
        uc3Button.setOnClickListener(v -> showFrameLayoutDemo());
        mainContainer.addView(uc3Button);

        // 添加UC4按钮
        Button uc4Button = new Button(this);
        uc4Button.setText("UC4 - 表格布局");
        uc4Button.setOnClickListener(v -> showTableLayoutDemo());
        mainContainer.addView(uc4Button);

        // 添加UC5按钮
        Button uc5Button = new Button(this);
        uc5Button.setText("UC5 - 网格布局");
        uc5Button.setOnClickListener(v -> showGridLayoutDemo());
        mainContainer.addView(uc5Button);
    }

    private void showLinearLayoutDemo() {
        mainContainer.removeAllViews();

        // 创建线性布局
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));
        linearLayout.setId(1);

        // 水平布局按钮
        Button horizontalButton = new Button(this);
        horizontalButton.setText("切换水平布局");
        horizontalButton.setOnClickListener(v -> {
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        });
        linearLayout.addView(horizontalButton);

        // 垂直布局按钮
        Button verticalButton = new Button(this);
        verticalButton.setText("切换垂直布局");
        verticalButton.setOnClickListener(v -> {
            linearLayout.setOrientation(LinearLayout.VERTICAL);
        });
        linearLayout.addView(verticalButton);

        // 左对齐按钮
        Button leftAlignButton = new Button(this);
        leftAlignButton.setText("左对齐");
        leftAlignButton.setOnClickListener(v -> {
            linearLayout.setGravity(Gravity.START);
        });
        linearLayout.addView(leftAlignButton);

        // 居中对齐按钮
        Button centerAlignButton = new Button(this);
        centerAlignButton.setText("居中对齐");
        centerAlignButton.setOnClickListener(v -> {
            linearLayout.setGravity(Gravity.CENTER);
        });
        linearLayout.addView(centerAlignButton);

        // 返回按钮
        Button backButton = new Button(this);
        backButton.setText("返回主菜单");
        backButton.setOnClickListener(v -> showMainMenu());
        linearLayout.addView(backButton);

        mainContainer.addView(linearLayout);
    }

    private void showRelativeLayoutDemo() {
        mainContainer.removeAllViews();

        // 创建相对布局
        RelativeLayout relativeLayout = new RelativeLayout(this);
        relativeLayout.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));

        // 左上角按钮
        Button topLeftButton = new Button(this);
        topLeftButton.setText("左上角");
        topLeftButton.setId(1);
        RelativeLayout.LayoutParams topLeftParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        topLeftParams.addRule(RelativeLayout.ALIGN_PARENT_START);
        topLeftParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        relativeLayout.addView(topLeftButton, topLeftParams);

        // 右上角按钮
        Button topRightButton = new Button(this);
        topRightButton.setText("右上角");
        topRightButton.setId(2);
        RelativeLayout.LayoutParams topRightParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        topRightParams.addRule(RelativeLayout.ALIGN_PARENT_END);
        topRightParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        relativeLayout.addView(topRightButton, topRightParams);

        // 左下角按钮
        Button bottomLeftButton = new Button(this);
        bottomLeftButton.setText("左下角");
        bottomLeftButton.setId(3);
        RelativeLayout.LayoutParams bottomLeftParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        bottomLeftParams.addRule(RelativeLayout.ALIGN_PARENT_START);
        bottomLeftParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        relativeLayout.addView(bottomLeftButton, bottomLeftParams);

        // 右下角按钮
        Button bottomRightButton = new Button(this);
        bottomRightButton.setText("右下角");
        bottomRightButton.setId(4);
        RelativeLayout.LayoutParams bottomRightParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        bottomRightParams.addRule(RelativeLayout.ALIGN_PARENT_END);
        bottomRightParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        relativeLayout.addView(bottomRightButton, bottomRightParams);

        // 中间按钮
        Button centerButton = new Button(this);
        centerButton.setText("中间");
        RelativeLayout.LayoutParams centerParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        centerParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        relativeLayout.addView(centerButton, centerParams);

        // 返回按钮
        Button backButton = new Button(this);
        backButton.setText("返回主菜单");
        RelativeLayout.LayoutParams backParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        backParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        backButton.setOnClickListener(v -> showMainMenu());
        relativeLayout.addView(backButton, backParams);

        mainContainer.addView(relativeLayout);
    }

    private void showFrameLayoutDemo() {
        mainContainer.removeAllViews();

        // 创建帧布局
        FrameLayout frameLayout = new FrameLayout(this);
        frameLayout.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));
        frameLayout.setId(1);

        // 使用颜色块替代图片
        View colorView = new View(this);
        colorView.setBackgroundColor(colors[0]);
        frameLayout.addView(colorView);

        // 返回按钮
        Button backButton = new Button(this);
        backButton.setText("返回主菜单");
        FrameLayout.LayoutParams backParams = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        backParams.gravity = Gravity.BOTTOM;
        backButton.setOnClickListener(v -> {
            handler.removeCallbacks(colorRunnable);
            showMainMenu();
        });
        frameLayout.addView(backButton, backParams);

        mainContainer.addView(frameLayout);

        // 开始颜色轮换
        handler.postDelayed(colorRunnable, 2000);
    }

    private void updateColor() {
        FrameLayout frameLayout = findViewById(1);
        if (frameLayout != null) {
            frameLayout.removeAllViews();

            // 使用不同颜色替代图片
            View colorView = new View(this);
            colorView.setBackgroundColor(colors[currentIndex]);
            frameLayout.addView(colorView);

            // 重新添加返回按钮
            Button backButton = new Button(this);
            backButton.setText("返回主菜单");
            FrameLayout.LayoutParams backParams = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            backParams.gravity = Gravity.BOTTOM;
            backButton.setOnClickListener(v -> {
                handler.removeCallbacks(colorRunnable);
                showMainMenu();
            });
            frameLayout.addView(backButton, backParams);

            currentIndex = (currentIndex + 1) % colors.length;
            handler.postDelayed(colorRunnable, 2000);
        }
    }

    private void showTableLayoutDemo() {
        mainContainer.removeAllViews();

        // 创建表格布局
        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));

        // 第一行
        TableRow row1 = new TableRow(this);
        Button button1 = new Button(this);
        button1.setText("按钮1");
        row1.addView(button1);

        Button button2 = new Button(this);
        button2.setText("按钮2");
        row1.addView(button2);

        tableLayout.addView(row1);

        // 第二行
        TableRow row2 = new TableRow(this);
        Button button3 = new Button(this);
        button3.setText("按钮3");
        row2.addView(button3);

        Button button4 = new Button(this);
        button4.setText("按钮4");
        row2.addView(button4);

        tableLayout.addView(row2);

        // 第三行 - 返回按钮
        TableRow row3 = new TableRow(this);
        Button backButton = new Button(this);
        backButton.setText("返回主菜单");
        TableRow.LayoutParams backParams = new TableRow.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT
        );
        backParams.span = 2;
        row3.addView(backButton, backParams);

        tableLayout.addView(row3);

        mainContainer.addView(tableLayout);
    }

    private void showGridLayoutDemo() {
        mainContainer.removeAllViews();

        // 创建网格布局
        GridLayout gridLayout = new GridLayout(this);
        gridLayout.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));
        gridLayout.setColumnCount(3);
        gridLayout.setRowCount(4);

        // 添加数字按钮
        for (int i = 1; i <= 9; i++) {
            Button button = new Button(this);
            button.setText(String.valueOf(i));
            GridLayout.Spec rowSpec = GridLayout.spec((i-1)/3);
            GridLayout.Spec colSpec = GridLayout.spec((i-1)%3);
            GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec, colSpec);
            gridLayout.addView(button, params);
        }

        // 添加0按钮
        Button zeroButton = new Button(this);
        zeroButton.setText("0");
        GridLayout.Spec rowSpec = GridLayout.spec(3);
        GridLayout.Spec colSpec = GridLayout.spec(1);
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec, colSpec);
        gridLayout.addView(zeroButton, params);

        // 添加返回按钮
        Button backButton = new Button(this);
        backButton.setText("返回主菜单");
        GridLayout.Spec backRowSpec = GridLayout.spec(4);
        GridLayout.Spec backColSpec = GridLayout.spec(0, 3);
        GridLayout.LayoutParams backParams = new GridLayout.LayoutParams(backRowSpec, backColSpec);
        backButton.setLayoutParams(backParams);
        backButton.setOnClickListener(v -> showMainMenu());
        gridLayout.addView(backButton);

        mainContainer.addView(gridLayout);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 停止颜色轮换
        handler.removeCallbacks(colorRunnable);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 如果当前是帧布局，恢复颜色轮换
        if (mainContainer.getChildAt(0) instanceof FrameLayout) {
            handler.postDelayed(colorRunnable, 2000);
        }
    }
}    